<template>
  <div id="content">
    <div id="bg">
      <div v-show="!isAdding">
        <div id="title">所有商品</div>
        <table id="form">
          <tr class="head">
            <th class="col_line">商品名字</th>
            <th class="col_line">仓库</th>
            <th class="col_line">数量</th>
            <th class="col_line">颜色</th>
            <!-- <th class="col_line">材质</th> -->
            <th class="col_line">进价</th>
            <th class="col_line">预估售价</th>
            <!-- <th class="col_line">备注</th> -->
            <th>更新时间</th>
          </tr>
          <tr
            v-for="(item, index) in info"
            :key="index"
            class="row"
            @click="updateGoods(item.id)"
          >
            <td class="col_line">{{ item.name }}</td>
            <td class="col_line">{{ item.storeName }}</td>
            <td class="col_line">{{ item.num }} {{ item.unit }}</td>
            <td class="col_line">{{ item.color }}</td>
            <!-- <td class="col_line">{{ item.texture }}</td> -->
            <td class="col_line">{{ item.prePurchasing }} 元</td>
            <td class="col_line">{{ item.preSelling }} 元</td>
            <!-- <td class="col_line">{{ item.description }}</td> -->
            <td>{{ item.updateTime }}</td>
          </tr>
          <tr>
            <div id="add" @click="addGoods">+</div>
          </tr>
        </table>
      </div>

      <div id="add_bg" v-show="isAdding">
        <div id="addGoods">
          <div class="line">
            <label for="name">商品：</label>
            <input autocomplete="off"
              v-model="name"
              name="name"
              type="text"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="storeName">仓库：</label>
            <select v-model="storeId" name="storeName">
              <option
                v-for="(item, index) in this.stores"
                :key="index"
                :value="item.id"
              >
                <div>{{ item.name }}</div>
              </option>
            </select>
          </div>

          <div class="line">
            <label for="num">库存：</label>
            <input autocomplete="off"
              type="text"
              name="num"
              v-model="num"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="unit">单位：</label>
            <input autocomplete="off"
              type="text"
              name="unit"
              v-model="unit"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="color">颜色：</label>
            <input autocomplete="off"
              type="text"
              name="color"
              v-model="color"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="texture">材质：</label>
            <input autocomplete="off" type="text" name="texture" v-model="texture" />
          </div>

          <div class="line">
            <label for="prePurchasing">进价：</label>
            <input autocomplete="off"
              type="text"
              name="prePurchasing"
              v-model="prePurchasing"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="preSelling">售价：</label>
            <input autocomplete="off"
              type="text"
              name="preSelling"
              v-model="preSelling"
              placeholder="(不能为空哦~)"
            />
          </div>

          <div class="line">
            <label for="description">备注：</label>
            <input autocomplete="off" type="text" name="description" v-model="description" />
          </div>
        </div>
        <input
          class="buttons"
          id="submit"
          type="button"
          @click="submitUpdate"
          value="提交"
        />
        <input
          class="buttons"
          id="cancel"
          type="button"
          @click="cancelSub"
          value="取消"
        />
      </div>
    </div>
    <div id="buttom"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  name: 'goods',
  computed: {
    ...mapState(['ownerId']),
  },
  data() {
    return {
      info: [],
      isAdding: false,
      id: '',
      storeId: '',
      name: '',
      num: '',
      unit: '',
      color: '',
      texture: '',
      prePurchasing: '',
      preSelling: '',
      description: '',
      stores: [],
      action: 'add',
    };
  },
  mounted() {
    this.getGoodsInfo();
    this.getAllStores();
  },
  methods: {
    addGoods() {
      this.isAdding = true;
      // console.log(this.num, ',', this.prePurchasing);
    },
    async updateGoods(id) {
      this.action = 'update';
      try {
        const res = await this.$axios.post(`http://work.caike.online/goods`, {
          action: 'queryGoods',
          id: id,
        });
        console.log('res_goods', res);
        if (res.data.status == 1) {
          const goods = res.data.info;
          this.id = id;
          this.storeId = goods.storeId;
          this.name = goods.name;
          this.num = goods.num;
          this.unit = goods.unit;
          this.color = goods.color;
          this.texture = goods.texture;
          this.prePurchasing = goods.prePurchasing;
          this.preSelling = goods.preSelling;
          this.description = goods.description;
          this.addGoods();
        }
      } catch (err) {
        this.$message({
          message: '查询失败',
          type: 'error',
          offset: 70,
        });
        console.log(err);
      }
    },
    async submitUpdate() {
      // console.log("pre=>",this.prePurchasing);
      if (this.validate_goods) {
        const updateTime = Date.parse(new Date()).toString().substr(0, 10);
        try {
          const res = await this.$axios.post(`http://work.caike.online/goods`, {
            action: this.action,
            id: this.id,
            ownerId: this.ownerId,
            storeId: this.storeId,
            name: this.name,
            num: this.num,
            unit: this.unit,
            color: this.color,
            texture: this.texture,
            prePurchasing: this.prePurchasing,
            preSelling: this.preSelling,
            description: this.description,
            updateTime: updateTime,
          });
          console.log(res);
          if (res.data.status == 1) {
            this.$message({
              message: '提交成功',
              type: 'success',
              offset: 70,
            }); 
            window.location.href = 'goods';
          }
        } catch (err) {
          this.$message({
            message: '查询失败',
            type: 'error',
            offset: 70,
          });
          console.log(err);
        }
      } else {
        this.$message({
          message: '数据为填写完整噢~',
          type: 'error',
          offset: 70,
        });
      }
    },
    validate_goods() {
      console.log('in vali');
      if (this.name.length == 0) {
        return false;
      } else if (this.storeId.length == 0) {
        return false;
      } else if (this.num.length == 0) {
        return false;
      } else if (this.unit.length == 0) {
        return false;
      } else if (this.color.length == 0) {
        return false;
      } else if (this.prePurchasing.length == 0) {
        return false;
      } else if (this.preSelling.length == 0) {
        return false;
      } else {
        return true;
      }
    },
    cancelSub() {
      this.goods = '';
      this.isAdding = false;
      window.location.href = '/goods';
    },
    async getAllStores() {
      try {
        const res = await this.$axios.post(`http://work.caike.online/store`, {
          action: 'queryAll',
          ownerId: this.ownerId,
        });
        console.log(res);
        if (res.data.status == 1) {
          this.stores = res.data.info;
          console.log('stores:', this.stores);
        }
      } catch (err) {
        this.$message({
          message: '查询失败',
          type: 'error',
          offset: 70,
        });
        console.log(err);
      }
    },
    async getGoodsInfo() {
      try {
        const res = await this.$axios.post(`http://work.caike.online/goods`, {
          action: 'queryAll',
          id: this.ownerId,
          chose: 0,
        });
        console.log(res);
        if (res.data.status == 1) {
          this.info = res.data.info;
          this.toTimeStamp();
        }
      } catch (err) {
        this.$message({
          message: '查询失败',
          type: 'error',
          offset: 70,
        });
        console.log(err);
      }
    },
    toTimeStamp(info) {
      this.info.forEach((item) => {
        // console.log('time => ', item.updateTime);
        item.updateTime = this.changeTime(item.updateTime);
        // console.log('time => ', item.updateTime);
      });
    },
    changeTime(sj) {
      sj = parseInt(sj) * 1000;
      const date = new Date(sj); // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      const dateNumFun = (num) => (num < 10 ? `0${num}` : num);
      const Y = `${date.getFullYear()}/`;
      const M = `${dateNumFun(date.getMonth() + 1)}/`;
      const D = `${dateNumFun(date.getDate())} `;
      const h = `${dateNumFun(date.getHours())}:`;
      const m = `${dateNumFun(date.getMinutes())}:`;
      const s = `${dateNumFun(date.getSeconds())}`;
      return Y + M + D + h + m + s;
    },
  },
};
</script>

<style lang="less" scoped>
#content {
  // width: 800px;
  // height: 100%;

  #bg {
    width: 100%;
    height: 100%;
    background-image: url('../assets/bg/3.jpeg');
    position: fixed;
    top: 0;
    left: 0;
    // z-index: -10;

    background-position: center 0;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    -webkit-background-size: cover;
    -o-background-size: cover;
    zoom: 1;
    opacity: 1;

    display: flex;
    flex-direction: column;
    padding-top: 90px;
  }

  #title {
    width: 850px;
    font-family: '楷体';
    margin: 0px auto;
    font-size: 30px;
    font-weight: bold;
    color: white;
    // position: fixed;
  }

  #add_bg {
    width: 50%;
    height: 450px;
    background-image: url('../assets/bg/3.jpeg');
    margin: 30px auto;

    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    -webkit-background-size: cover;
    -o-background-size: cover;
    zoom: 1;
    opacity: 1;
    border-radius: 25px;
    box-shadow: black 10px 10px 20px;
    transition: all 1s ease;

    #addGoods {
      display: flex;
      flex-direction: column;
      justify-content: start;
      color: rgba(255, 255, 255, 0.6);
      font-size: 18px;
      padding-top: 10px;

      .line {
        width: 80%;
        margin: auto;
        // text-align: start;
        display: flex;
        flex-direction: row;
        justify-content: center;

        label {
          // text-align: right;
          padding: 10px 0;
          // width:40%;
          font-size: 16px;
          color: rgba(255, 255, 255, 0.6);
          pointer-events: none;
          transition: 0.5s;
        }

        input {
          width: 60%;
          padding: 10px 0;
          font-size: 14px;
          color: rgba(255, 255, 255, 0.6);
          letter-spacing: 1px;
          margin-bottom: 5px;
          border: none;
          border-bottom: 1px solid #fff;
          outline: none;
          background: transparent;
        }

        select {
          color: rgba(255, 255, 255, 0.6);
          width: 60%;
          background-color: transparent;
          border: none;
          border-bottom: 1px solid white;

          option {
            color: black;
            background-color: transparent;
          }
        }

        // input:hover {
        //   background: transparent;
        //   border: none;
        //   outline: none;
        //   font-size: 16px;
        //   color: #fff;
        //   padding: 15px 20px;
        //   cursor: pointer;
        //   border-radius: 10px;
        // }

        // input:focus ~ label,
        // input:valid ~ label {
        //   top: -18px;
        //   left: 0;
        //   color: #03a9f4;
        //   font-size: 14px;
        // }
      }
    }
  }

  #form {
    width: 1000px;
    margin: 20px auto;
    font-family: '楷体';
    font-size: 16px;
    color: white;
    // padding: 20px;
    // padding-top: 0px;
    padding: 0px;
    background-color: rgba(0, 0, 0, 0.3);
    box-shadow: 0px 15px 25px rgba(0, 0, 0, 0.5);
    border-radius: 20px;

    .head {
      font-weight: bold;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.5);
    }

    .col_line {
      border-right: 1px rgba(255, 255, 255, 0.527) dashed;
    }

    .option {
      display: flex;
      direction: row;
      justify-content: space-around;
    }

    .row:hover {
      // height: 40px;
      // font-size: 18px;
      // font-weight: bold;
      // transition: all 1s ease;
      cursor: pointer;
    }

    th {
      height: 40px;
      border-bottom: 1px rgba(255, 255, 255, 0.527) dashed;
    }

    tr {
      height: 30px;
    }
    #add {
      width: 50px;
      margin: 5px auto;
      border-radius: 10%;
      color: white;
      border: dashed 1px white;
      cursor: pointer;
    }
  }
  .buttons {
    margin: 5px 10px;
    color: white;
    border-radius: 10px;
    background-color: transparent;
  }
  .buttons:hover {
    cursor: pointer;
    box-shadow: 1px 1px 3px grey;
    transition: all 0.8s ease;
  }
}
// #update:hover #port:hover {
//   font-size: 120%;
//   color: turquoise;
//   cursor: pointer;
//   transition: all 0.8s ease;
// }
</style>